package com.android.takegism.ui;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.takegism.R;
import com.android.takegism.utils.Clue;

import java.util.LinkedList;

public class ClueAdapter extends BaseAdapter {

    private Context mContext;
    private LinkedList<Clue> mData;

    public ClueAdapter() {
    }

    public ClueAdapter(LinkedList<Clue> mData, Context mContext) {
        this.mData = mData;
        this.mContext = mContext;
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.clue_item, parent, false);
            holder = new ViewHolder();
            holder.img_icon = (ImageView) convertView.findViewById(R.id.clue_item_image);
            holder.txt_content = (TextView) convertView.findViewById(R.id.clue_item_text);

            int width = new DisplayMetrics().widthPixels / 2;
            holder.img_icon.setMaxWidth(width);
            holder.txt_content.setWidth(width);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        System.out.println(holder.img_icon);
        System.out.println(holder.txt_content);

//        holder.img_icon.setBackground(mData.get(position).getImageId());
        holder.img_icon.setImageResource(mData.get(position).getImageId());
        holder.txt_content.setText(mData.get(position).getClue());
        return convertView;
    }

    //添加一个元素
    public void add(Clue data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(data);
        notifyDataSetChanged();
    }

    //往特定位置，添加一个元素
    public void add(int position, Clue data) {
        if (mData == null) {
            mData = new LinkedList<>();
        }
        mData.add(position, data);
        notifyDataSetChanged();
    }

    public void remove(Clue data) {
        if (mData != null) {
            mData.remove(data);
        }
        notifyDataSetChanged();
    }

    public void remove(int position) {
        if (mData != null) {
            mData.remove(position);
        }
        notifyDataSetChanged();
    }

    public void clear() {
        if (mData != null) {
            mData.clear();
        }
        notifyDataSetChanged();
    }

    private class ViewHolder {
        ImageView img_icon;
        TextView txt_content;
    }

}